<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class milik extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model 
        $this->load->model('m_milik'); 
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
        // load helpers
    }

    // welcome administrator
    public function index() {
        // set template content
        $this->smarty->assign("template_content", "master/milik/list.html");

        //pencarian
        $search = $this->tsession->userdata('master_milik_search'); 
        $this->smarty->assign("search", $search);
        
        $milik_name = empty($search['milik_name']) ? '%%' : '%' . $search['milik_name'] . '%'; 

        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("master/milik/index/"); 
        
        $params = array($milik_name);
        $config['total_rows'] = $this->m_milik->get_count_all_milik_search($params); 
        $config['uri_segment'] = 4;
        $config['per_page'] = 10;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($milik_name, ($start - 1), $config['per_page']);
        $this->smarty->assign("rs_id", $this->m_milik->get_all_milik_pagination_search($params));
        // milik
        $rs_milik =  $this->m_milik->get_all_milik();
        $this->smarty->assign("rs_milik", $rs_milik);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function proses_cari() {
        // set page rules
        $this->_set_page_rule("R");
        // data
        if ($this->input->post('save') == "Reset") {
            $this->tsession->unset_userdata('master_milik_search');
        } else {
            $params = array(
                "milik_name"    => $this->input->post("milik_name") 
            );
            $this->tsession->set_userdata("master_milik_search", $params);
        }
        // redirect
        redirect("master/milik");
    }

    public function add() {
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/milik/add.html");
        // milik
        $rs_milik =  $this->m_milik->get_all_milik();
        $this->smarty->assign("rs_milik", $rs_milik);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // add process
    public function add_process() {
        // cek input   
        $this->tnotification->set_rules('milik_name', 'Nama', 'trim|required|max_length[100]'); 
        // process 
        if ($this->tnotification->run() !== FALSE) { 
            // parameter
            $params = array(  
                $this->input->post('milik_name'), 
                $this->com_user['user_id']
            );
            
            if(!$this->m_milik->is_exist_milik($this->input->post('milik_name'))){
                // insert
                if ($this->m_milik->insert($params)) {
                    // notification
                    $this->tnotification->delete_last_field();
                    $this->tnotification->sent_notification("success", "Data berhasil disimpan");
                } else {
                    // default error
                    $this->tnotification->sent_notification("error", "Data gagal disimpan");
                }
            }else{
                // default error
                $this->tnotification->sent_notification("error", "Nama milik telah ada");
            } 
        } 
        // default redirect
        redirect("master/milik/add");
    }

    public function edit($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/milik/edit.html");
        //data
        $result = $this->m_milik->get_milik_by_id($id);
        $this->smarty->assign("result", $result); 
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function edit_process() {
        // cek input 
        $this->tnotification->set_rules('milik_cd', 'Kode', 'trim|required');
        $this->tnotification->set_rules('milik_name', 'Nama', 'trim|required|max_length[100]'); 
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array(
                $this->input->post('milik_name'), 
                $this->com_user['user_id'],
                $this->input->post('milik_cd')
            );
            // insert
            if ($this->m_milik->edit($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil disimpan");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal disimpan");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal disimpan");
        }
        // default redirect
        redirect("master/milik/edit/" . $this->input->post('milik_cd'));
    }

    public function delete($id) {
        // $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "master/milik/delete.html");
        //data
        $result = $this->m_milik->get_milik_by_id($id);
        $this->smarty->assign("result", $result); 
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    public function delete_process() {
        // cek input 
        $this->tnotification->set_rules('milik_cd', 'Kode', 'trim|required|max_length[3]');
        // process
        if ($this->tnotification->run() !== FALSE) {
            // parameter
            $params = array($this->input->post('milik_cd')
            );
            // insert
            if ($this->m_milik->delete($params)) {
                // notification
                $this->tnotification->delete_last_field();
                $this->tnotification->sent_notification("success", "Data berhasil dihapus");
            } else {
                // default error
                $this->tnotification->sent_notification("error", "Data gagal dihapus");
            }
        } else {
            // default error
            $this->tnotification->sent_notification("error", "Data gagal dihapus");
        }
        // default redirect
        redirect("master/milik/");
    }

}
